package com.fresh.bus.chat.ctrl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;

import com.fresh.bus.canoe.command.annotation.CanoeCommand;
import com.fresh.bus.canoe.message.Request;
import com.fresh.bus.canoe.message.Response;
import com.fresh.bus.canoe.session.Session;
import com.fresh.bus.chat.business.WealthService;
import com.fresh.bus.chat.util.MessageUtils;

@Controller("friendCtrl")
public class FriendCtrl extends BaseCtrl {

	@CanoeCommand(value = OpCodes.GUANZHU_TA)
	public Response guanzhuTa(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.GUANZHU_TA);

		try {
			String nikeName = (String) data.get("nikeName");
			String user = (String) data.get("toUser");
			Integer toUser = Integer.valueOf(user);
			Map<String, String> map = friendService.addFriend(
					Integer.valueOf(id), toUser, nikeName);
			wealthService.update(1, WealthService.ADD_GUANZHU, user);
			//expService.update(session, data, OpCodes.GUANZHU_TA);
			response.setData(map);
		} catch (Exception e) {
			log.error("User[" + id + "]", e);
			response.setData(e.getMessage());
		}
		return response;
	}

	@CanoeCommand(value = OpCodes.FRIEND_LIST)
	public Response friendList(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.FRIEND_LIST);
		try {
			List<Map<String, String>> l = friendService.queryFriends(id);
			response.setData(l.toArray());
			return response;
		} catch (Exception e) {
			log.error("User[" + id + "]", e);
			this.reportError(session);
		}
		return null;
	}
	
	@CanoeCommand(value = OpCodes.READ_FANS_LIST)
	public Response fansList(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.READ_FANS_LIST);
		try {
			List<Map<String, String>> l = friendService.queryFans(id);
			if(l.isEmpty()){
				response.setData(false);
			}else{
				response.setData(l.toArray());
			}
			return response;
		} catch (Exception e) {
			log.error("User[" + id + "]", e);
			this.reportError(session);
		}
		return null;
	}	

	@CanoeCommand(value = OpCodes.CANCEL_GUANZHU)
	public Response removeFriend(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String from = session.getRole().getId();
		String to = (String) data.get("to");
		Response response = new Response(OpCodes.CANCEL_GUANZHU);
		try {
			boolean result = friendService.removeFriend(Integer.valueOf(from),
					Integer.valueOf(to));
			wealthService.update(1, WealthService.REMOVE_GUANZHU, to);
			response.setData(true);
			return response;
		} catch (Exception e) {
			log.error("User[" + from + "] to User[:" + to + "]", e);
			this.reportError(session);
		}
		return null;
	}

	@CanoeCommand(value = OpCodes.SAY_HELLO)
	public Response sayHello(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String from = session.getRole().getId();
		String to = (String) data.get("to");
		Response response = new Response(OpCodes.SAY_HELLO);
		try {
			friendService.sayHello(from, to);
			response.setData(true);
			return response;
		} catch (Exception e) {
			log.error("sayHello[" + from + "] to User[:" + to + "]", e);
			this.reportError(session);
		}
		return null;
	}
	
	@CanoeCommand(value = OpCodes.REQ_FRIEND)
	public Response reqFriend(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String from = session.getRole().getId();
		String to = (String) data.get("to");
		int flowerAmt = (Integer) data.get("i");
		Response response = new Response(OpCodes.REQ_FRIEND);
		try {
			boolean flag = friendService.reqFriend(from, to,flowerAmt);
			if(flag){
				Map<String,String> f = friendService.friend(from, to);
				response.setData(f);
				return response;
			}
		} catch (Exception e) {
			log.error("reqFriend[" + from + "] to User[:" + to + "]", e);
			this.reportError(session);
		}
		return null;
	}	

	@CanoeCommand(value = OpCodes.ACCEPT_FRIEND)
	public Response acceptFriend(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String from = session.getRole().getId();
		String to = (String) data.get("to");
		int level = (Integer)data.get("l");
		
		Response response = new Response(OpCodes.ACCEPT_FRIEND);
		try {
			boolean flag = friendService.acceptFriend(from, to,level);
			response.setData(flag);
			return response;
		} catch (Exception e) {
			log.error("acceptFriend[" + from + "] to User[:" + to + "]", e);
			this.reportError(session);
		}
		return null;
	}	
	
	@CanoeCommand(value = OpCodes.REQ_LEVEL_UP)
	public Response inviteLevelUp(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String from = session.getRole().getId();
		String to = (String) data.get("to");
		Response response = new Response(OpCodes.REQ_LEVEL_UP);
		try {
			friendService.inviteLevelUp(from, to);
			response.setData(true);
			return response;
		} catch (Exception e) {
			log.error("inviteLevelUp[" + from + "] to User[:" + to + "]", e);
			this.reportError(session);
		}
		return null;
	}
	
	@CanoeCommand(value = OpCodes.REQ_NEW_TREE)
	public Response inviteNewTree(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String from = session.getRole().getId();
		String to = (String) data.get("to");
		Response response = new Response(OpCodes.REQ_NEW_TREE);
		try {
			friendService.inviteNewTree(from, to);
			response.setData(true);
			return response;
		} catch (Exception e) {
			log.error("inviteNewTree[" + from + "] to User[:" + to + "]", e);
			this.reportError(session);
		}
		return null;
	}	

	@CanoeCommand(value = OpCodes.REQ_WATER)
	public Response inviteGiveWater(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String from = session.getRole().getId();
		String to = (String) data.get("to");
		Response response = new Response(OpCodes.REQ_WATER);
		try {
			boolean flag = friendService.inviteGiveWater(from, to);
			response.setData(flag);
			return response;
		} catch (Exception e) {
			log.error("inviteNewTree[" + from + "] to User[:" + to + "]", e);
			this.reportError(session);
		}
		return null;
	}	
	
	@CanoeCommand(value = OpCodes.THANK_WATER)
	public Response thankWater(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String from = session.getRole().getId();
		String to = (String) data.get("to");
		Response response = new Response(OpCodes.THANK_WATER);
		try {
			friendService.thankWater(from, to);
			response.setData(true);
			return response;
		} catch (Exception e) {
			log.error("thankWater[" + from + "] to User[:" + to + "]", e);
			this.reportError(session);
		}
		return null;
	}	
	
	@CanoeCommand(value = OpCodes.UPGREADE_ASSO)
	public Response upgradeAsso(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String from = session.getRole().getId();
		String to = (String) data.get("to");
		Response response = new Response(OpCodes.UPGREADE_ASSO);
		try {
			friendService.upgradeAsso(from, to);
			Map<String,String> map = friendService.friend(from, to);
			response.setData(map);
			return response;
		} catch (Exception e) {
			log.error("sayHello[" + from + "] to User[:" + to + "]", e);
			this.reportError(session);
		}
		return null;
	}	

	@CanoeCommand(value = OpCodes.WRITE_MSG)
	public Response writeMessage(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.WRITE_MSG);
		try {
			String to = (String) data.get("t");
			String content = (String) data.get("c");
			boolean isReply = (Boolean) data.get("r");

			int permitAmt = this.friendService.writeMessage(id, to, content,
					isReply);
			if (!isReply) {
				expService.update(session, data, OpCodes.WRITE_MSG);
				if(permitAmt==-1){
					response.setData("今日可留言数已用完!更多留言需等明日。");
				}else{
					response.setData(permitAmt);
				}
			} else {
				response.setData(true);
			}
			// expService.update(session, data, OpCodes.USER_REPORT);
		} catch (Exception e) {
			log.error("User[" + id + "]:", e);
			response.setData(MessageUtils.getErrorMessage());
		}
		return response;
	}

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.TALK_HISTORY)
	public Response talkHistory(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String id = session.getRole().getId();
		String to = (String) data.get("u");
		Response response = new Response(OpCodes.TALK_HISTORY);
		try {
			List<Map<String, Object>> msgs = this.friendService.readHistory(id,
					to);
			response.setData(msgs.toArray());
		} catch (Exception e) {
			log.error("User[" + id + "] and user[" + to
					+ "] talkhistory failed...", e);
			response.setData(e.getMessage());
		}
		return response;
	}
}
